from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404
from django.views.decorators.http import require_http_methods, require_POST

from . import models


def index(request):
    books = models.Book.objects.all()
    context = {'books': books}
    return render(request, "app2/index.html", context=context)


@require_http_methods(['GET', 'POST'])
def detail(request, pk):
    book = get_object_or_404(models.Book, pk=pk)
    lines = []
    lines.append('<p>name: %s</p>' % book.name)
    lines.append('<p>author: %s</p>' % book.author)
    lines.append('<p>price: %s</p>' % book.price)
    lines.append('<p>year: %s</p>' % book.year)
    text = '\n'.join(lines)
    return HttpResponse(text)
